package com.company.huisuo.biz.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.company.huisuo.base.BaseSingleAdapter
import com.company.huisuo.biz.bean.MerchantDanceBean
import com.company.huisuo.databinding.ItemKroomDanceBinding
import com.company.huisuo.ext.loadImg
import com.company.huisuo.ext.loadImageCustom
import com.company.huisuo.ext.toast

/**
 * K房团舞列表适配器
 */
class KRoomDanceAdapter : BaseSingleAdapter<MerchantDanceBean, ItemKroomDanceBinding>() {

    private var mSelectPos = 0

    override val initViewBinding: (LayoutInflater, ViewGroup, Boolean) -> ItemKroomDanceBinding
        get() = ItemKroomDanceBinding::inflate

    override fun convert(
        holder: SingleViewHolder<ItemKroomDanceBinding>, position: Int, item: MerchantDanceBean?
    ) {
        holder.binding.viewBorder.isVisible = mSelectPos == position
        
        // 加载舞蹈封面图片，如果URL为空或无效，则使用默认占位图
        if (item?.danceCover.isNullOrEmpty()) {
            holder.binding.ivDanceCover.loadImg(com.company.huisuo.R.drawable.ic_kroom_dance_2)
        } else {
            // 使用自定义的图片加载方法，添加错误处理
            holder.binding.ivDanceCover.loadImageCustom(item?.danceCover) {
                // 添加错误处理，如果加载失败则显示默认图片
                error(com.company.huisuo.R.drawable.ic_kroom_dance_2)
                placeholder(com.company.huisuo.R.drawable.ic_kroom_dance_2)
                centerCrop()
                timeout(30000)
            }
        }
        
        holder.binding.tvDanceName.text = item?.danceName ?: ""
        holder.binding.tvPrice.text = item?.dancePrice?.toInt().toString() ?: "0"
        holder.binding.tvYuyue.setOnClickListener {
            "预约成功".toast()
            mSelectPos = position
            notifyDataSetChanged()
        }
    }
}
